﻿using System;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0499 //2010.03.17 //Begin
namespace InfoJetSoft.Service.Worker
{
	class XSNViewFormatInkPictureWorker
	{
		public static void formatInkPicture(View view)
		{
			string xdPrefix = view.XdPrefix;
			string xdBackgroundPictureName = NamespaceUtil.toFullName(xdPrefix, "backgroundPicture");
            //CID 0860 //2011.04.14 //Begin
            string xslStyleFilter = NamespaceUtil.toFullName(view.XslPrefix, "attribute") + "[@name='style']";
            //CID 0860 //2011.04.14 //End

			XmlElement root = view.Doc.DocumentElement;
			XmlNodeList inkElementList = root.SelectNodes( "//object[@classid='clsid:04a1e553-fe36-4fde-865e-344194e69424']" );
			foreach( XmlElement inkElement in inkElementList )
			{
				string style = inkElement.GetAttribute( "style" );
                //CID 0860 //2011.04.14 //Begin
                if (style.Length <= 0)
                {
                    XmlElement styleElement = (XmlElement)DOMUtil.selectANode(inkElement, xslStyleFilter, view.NsResolver);
                    if (styleElement != null && styleElement.FirstChild is XmlText)
                    {
                        style = styleElement.FirstChild.Value;
                    }
                }
                //CID 0860 //2011.04.14 //End
                int[] size = parseStyleSize(style);
				if( size != null )
				{
					XmlElement inkPictureElement = DOMUtil.renameElementName( inkElement, "img" );
					XmlElement parentElement = (XmlElement)inkPictureElement.ParentNode;
					parentElement.RemoveChild( inkPictureElement );
					XmlElement divElement = view.Doc.CreateElement("div");
					parentElement.AppendChild( divElement );

					bool hasBackground = false;
					string backgroundPicture = inkPictureElement.GetAttribute( xdBackgroundPictureName );
					if( backgroundPicture != null && backgroundPicture.Length > 0 )
					{
						XmlElement backgroundImgElement = view.Doc.CreateElement( "img" );
						backgroundImgElement.SetAttribute( "isBackground", "true" );
						backgroundImgElement.SetAttribute( "src", backgroundPicture );
						if( style != null && style.Length > 0 )
						{
							backgroundImgElement.SetAttribute( "style", "width:" + size[0] + "px;height:" + size[1] + "px;margin-top:-2px;margin-bottom:3px;" );
						}
						divElement.AppendChild( backgroundImgElement );
						hasBackground = true;
					}

					int inkWidth = size[0] + 2;
					int inkHeight = size[1] + 2;

					string inkStyle = ";border:#dcdcdc 1px solid;background-color:transparent;width:" + inkWidth + "px;height:" + inkHeight + "px;";
					if( hasBackground )
					{
						inkStyle = inkStyle + "margin-left:-" + inkWidth + "px;";
					}
					inkPictureElement.SetAttribute("style", inkStyle);
					inkPictureElement.RemoveAttribute("class");
					inkPictureElement.SetAttribute("alt", "Click here to edit ink picture");

                    //CID 0860 //2011.04.14 //Begin
                    XmlElement attrStyleElement = (XmlElement)DOMUtil.selectANode(inkPictureElement, xslStyleFilter, view.NsResolver);
                    if (attrStyleElement != null)
                    {
                        if (attrStyleElement.FirstChild is XmlText)
                        {
                            XmlText text = attrStyleElement.FirstChild as XmlText;
                            text.Value = inkStyle;
                        }
                    }
                    //CID 0860 //2011.04.14 //End

					divElement.AppendChild( inkPictureElement );
				}
			}
		}

		private static char[] SplitStyleChars = new char[]{ ';' };
		private static char[] SplitRuleChars = new char[]{ ':' };

		private static int[] parseStyleSize( string style )
		{
			int width = -1;
			int height = -1;
			string[] rules = style.Split( SplitStyleChars );
			foreach( string rule in rules )
			{
				string[] keyValue = rule.Split( SplitRuleChars );
				if( keyValue.Length == 2 )
				{
					string key = keyValue[0].Trim().ToLower();
					string val = keyValue[1].Trim().ToLower().Replace( "px", "" );
					if( key.Equals( "width" ) )
					{
						width = int.Parse( val );
					}
					if( key.Equals( "height" ) )
					{
						height = int.Parse( val );
					}
				}
			}
			if( width > 0 && height > 0 )
			{
				return new int[]{ width, height };
			}
			return null;
		}
	}
}
//CID 0499 //2010.03.17 //End